// pages/editarea/page.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selAreaUrl:'http://127.0.0.1:8082/demo/admin/listbyid',
    addAreaUrl:'http://127.0.0.1:8082/demo/admin/add',
    updAreaUrl:'http://127.0.0.1:8082/demo/admin/upd',
    areaId:undefined,
    areaName:'',
    priority:undefined
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({areaId:options.id})
    if (options.id) {
      wx.request({
        url: that.data.selAreaUrl,
        method:'GET',
        data:{'id':options.id},
        success:function(res){
          console.log(res.data);
          var area = res.data.area;
          if (area) {
            that.setData({
              areaId:area.areaId,
              areaName:area.areaName,
              priority:area.priority,
            });
          } else {
            var msg = '获取数据失败！' + res.data.errMsg;
            wx.showToast({
              title: msg,
              icon:'',
              duration:2000 // 2秒后消失
            });
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit:function(e){
    var that = this;
    var setJson = e.detail.value;
    var url = that.data.addAreaUrl;
    if (that.data.areaId) {
      setJson.areaId = that.data.areaId;
      url = that.data.updAreaUrl;
    }
    wx.request({
      url: url,
      method:'POST',
      data:setJson,
      success:function(res){
        console.log(res.data);
        var success = res.data.success;
        var msg = '操作成功！';
        if (!success) {
          msg = '操作失败！' + res.data.errMsg;
        }
        wx.showToast({
          title: msg,
          icon:'',
          duration:2000 // 2秒后消失
        });
        if (success) {
          wx.navigateTo({
            url: '../arealist/list',
          });
        }
      }
    })
  }
})